package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送小视频评分消息，工具类
 */
@Component
public class VideoMQService {

    @Reference
    private VideoApi videoApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送消息
     * type 类型 1-发布小视频，2-点赞，3-取消点赞，4-评论
     */
    public void sendMsg(String videoId, Integer type){
        // 定义消息内容
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("userId", UserHolder.getUserId());
        map.put("videoId", videoId);
        Video video = videoApi.findById(videoId);
        map.put("vid", video.getVid());

        // 发送消息
        rocketMQTemplate.convertAndSend("tanhua-video", JSON.toJSONString(map));

    }
}
